from multiprocessing.connection import Client

from django.shortcuts import render, redirect
from django.http import HttpResponse
from u4xg_site import settings
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm
from django.contrib.auth import authenticate
from django.contrib.auth import login as dlogin
from django.contrib.auth import logout as dlogout

import time
from . import u4xg, u4xg_forms


''' default / home / landing-page view '''

def default( request ):
    
    if request.user.is_authenticated:
        return default_authenticated(request)
    else:
        return default_anonymous(request)

''' authenticated views '''

def default_authenticated( request ):
    uname = request.user.username
    password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
    username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
    email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
    context = {
        'password_update_form': password_update_form,
        'username_change_form': username_change_form,
        'email_change_form': email_change_form,
        'uname': uname,
        'manual_display': 'block',
        'publicgames_display': 'none',
        'currentgames_display': 'none', 
        'account_display': 'none',
    }
    return render( request, 'u4xg_game/default_authenticated.html', context )

def account( request ):
    if not request.user.is_authenticated:
        return redirect( 'login' )
    uname = request.user.username
    last_login = request.user.last_login
    password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
    username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
    email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
    context = {
        'password_update_form': password_update_form,
        'username_change_form': username_change_form,
        'email_change_form': email_change_form,
        'uname': uname,
        'last_login': last_login,
        'manual_display': 'none',
        'publicgames_display': 'none',
        'currentgames_display': 'none', 
        'account_display': 'block',
    }
    return render( request, 'u4xg_game/default_authenticated.html', context )

def publicgames( request ):
    if not request.user.is_authenticated:
        return redirect( 'login' )
    uname = request.user.username
    password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
    username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
    email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
    context = {
        'password_update_form': password_update_form,
        'username_change_form': username_change_form,
        'email_change_form': email_change_form,
        'uname': uname,
        'manual_display': 'none',
        'publicgames_display': 'block',
        'currentgames_display': 'none', 
        'account_display': 'none',
    }
    return render( request, 'u4xg_game/default_authenticated.html', context )

def currentgames( request ):
    if not request.user.is_authenticated:
        return redirect( 'login' )
    uname = request.user.username
    password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
    username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
    email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
    context = {
        'password_update_form': password_update_form,
        'username_change_form': username_change_form,
        'email_change_form': email_change_form,
        'uname': uname,
        'manual_display': 'none',
        'publicgames_display': 'none',
        'currentgames_display': 'block', 
        'account_display': 'none',
    }
    return render( request, 'u4xg_game/default_authenticated.html', context )

''' change password views '''

def account_change_password( request ):
    if request.user.is_authenticated:
        if request.method == 'POST':
            return account_change_password_post( request )
        if request.method == "GET":
            return account_change_password_get( request )
    else:
        return redirect( 'login' )
    
    return redirect( 'default' ) 

def account_change_password_get( request ):
    return redirect( 'account' )

def account_change_password_post( request ):
    uname = request.user.username
    password_update_form = u4xg_forms.PasswordUpdateForm( request.user, data=request.POST )
    if password_update_form.is_valid():
        password_update_form.save()
        return redirect( 'login' )
    else:
        username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
        email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
        context = {
            'password_update_form': password_update_form,
            'username_change_form': username_change_form,
            'email_change_form': email_change_form,
            'uname': uname,
            'manual_display': 'none',
            'publicgames_display': 'none',
            'currentgames_display': 'none', 
            'account_display': 'block',
        }
        return render( request, 'u4xg_game/default_authenticated.html', context )

''' change username views '''

def account_change_username( request ):
    if request.user.is_authenticated:
        if request.method == 'POST':
            return account_change_username_post( request )
        if request.method == "GET":
            return account_change_username_get( request )
    else:
        return redirect( 'login' )
    
    return redirect( 'default' ) 

def account_change_username_get( request ):
    return redirect( 'account' )

def account_change_username_post( request ):
    uname = request.user.username
    received_form = u4xg_forms.UsernameChangeForm(data=request.POST, instance=request.user)
    if received_form.is_valid():
        received_form.save()
        return redirect('account')
    else:
        password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
        username_change_form = received_form
        email_change_form = u4xg_forms.EmailChangeForm( instance=request.user)
        context = {
            'password_update_form': password_update_form,
            'username_change_form': username_change_form,
            'email_change_form': email_change_form,
            'uname': uname,
            'manual_display': 'none',
            'publicgames_display': 'none',
            'currentgames_display': 'none', 
            'account_display': 'block',
        }
        return render( request, 'u4xg_game/default_authenticated.html', context )

''' change email views '''

def account_change_email( request ):
    if request.user.is_authenticated:
        if request.method == 'POST':
            return account_change_email_post( request )
        if request.method == "GET":
            return account_change_email_get( request )
    else:
        return redirect( 'login' )
    
    return redirect( 'default' ) 

def account_change_email_post( request ):
    uname = request.user.username
    received_form = u4xg_forms.EmailChangeForm(data=request.POST, instance=request.user)
    if received_form.is_valid():
        received_form.save()
        return redirect('account')
    else:
        password_update_form = u4xg_forms.PasswordUpdateForm( request.user )
        username_change_form = u4xg_forms.UsernameChangeForm( instance=request.user)
        email_change_form = received_form
        context = {
            'password_update_form': password_update_form,
            'username_change_form': username_change_form,
            'email_change_form': email_change_form,
            'uname': uname,
            'manual_display': 'none',
            'publicgames_display': 'none',
            'currentgames_display': 'none', 
            'account_display': 'block',
        }
        return render( request, 'u4xg_game/default_authenticated.html', context )

def account_change_email_get( request ):
    return redirect( 'account' )

''' anonymous views '''

def default_anonymous( request ): 
    registration_form = u4xg_forms.RegistrationForm()
    login_form = u4xg_forms.LoginForm()
    context = {
        'about_display': 'block',
        'login_display': 'none',
        'register_display': 'none',
        'registration_form': registration_form,
        'login_form': login_form,
    }
    return render( request, 'u4xg_game/default_anonymous.html', context )

''' register views '''

def register( request ):
    if request.user.is_authenticated:
        return redirect('default')
    
    if request.method == 'POST':
        return register_post( request )
    if request.method == "GET":
        return register_get( request )
    
    return redirect( 'default' ) 

def register_post( request ):

    received_form = u4xg_forms.RegistrationForm(request.POST)
    if received_form.is_valid():
        received_form.save()
        return redirect('login')
    else:
        registration_form = received_form
        login_form = u4xg_forms.LoginForm()
        context = {
            'about_display': 'none',
            'login_display': 'none',
            'register_display': 'block',
            'registration_form': registration_form,
            'login_form': login_form,
        }
        return render( request, 'u4xg_game/default_anonymous.html', context )

def register_get( request ):
    
    registration_form = u4xg_forms.RegistrationForm()
    login_form = u4xg_forms.LoginForm()
    context = {
        'about_display': 'none',
        'login_display': 'none',
        'register_display': 'block',
        'registration_form': registration_form,
        'login_form': login_form,
    }

    return render( request, 'u4xg_game/default_anonymous.html', context )

''' login views '''


def login( request ):
    if request.user.is_authenticated:
        return redirect('default')
   
    if request.method == 'POST':
        return login_post( request )
    if request.method == 'GET':
        return login_get( request )

    return redirect( 'default' )

def login_post( request ):
    received_form = u4xg_forms.LoginForm(data=request.POST)

    if received_form.is_valid(): #not sure if there is implicit authentication here
        username = received_form.cleaned_data.get('username')
        raw_password = received_form.cleaned_data.get('password')
        user = authenticate(request, username=username, password=raw_password)
        if user != None:
            dlogin(request, user)
            return redirect('default')
        else:
            login_form = received_form
            registration_form = u4xg_forms.RegistrationForm()
            context = {
                'about_display': 'none',
                'login_display': 'block',
                'register_display': 'none',
                'registration_form': registration_form,
                'login_form': login_form,
            }
            return render( request, 'u4xg_game/default_anonymous.html', context )
    else:
        print( "login form isnt valid" )
        login_form = received_form
        registration_form = u4xg_forms.RegistrationForm()
        context = {
            'about_display': 'none',
            'login_display': 'block',
            'register_display': 'none',
            'registration_form': registration_form,
            'login_form': login_form,
        }
        return render( request, 'u4xg_game/default_anonymous.html', context )

def login_get( request ):
    
    registration_form = u4xg_forms.RegistrationForm()
    login_form = u4xg_forms.LoginForm()
    context = {
        'about_display': 'none',
        'login_display': 'block',
        'register_display': 'none',
        'registration_form': registration_form,
        'login_form': login_form,
    }

    return render( request, 'u4xg_game/default_anonymous.html', context )

''' logout view '''

def logout( request ):
    dlogout( request )
    return redirect( 'login' )

